import os
import re
import pandas as pd

# Read the CSV file
df = pd.read_csv('../data/gear-store.csv')
# Function to create a valid filename
def create_valid_filename(s):
    # Remove invalid characters and replace spaces with underscores
    s = re.sub(r'[^\w\-_\. ]', '', s)
    return s.replace(' ', '_')

# Create the directory if it doesn't exist
os.makedirs('../data/product', exist_ok=True)

# Iterate through each row in the DataFrame
for index, row in df.iterrows():
    # Create filename using name, category, and subcategory
    filename = f"{create_valid_filename(row['name'])}_{create_valid_filename(row['category'])}_{create_valid_filename(row['subcategory'])}.txt"

    print(f"Creating file {filename}, current index {index}")
    # Full path for the file
    filepath = os.path.join('../data/product', filename)

    # Create the content for the file
    content = f"Name: {row['name']}\n"
    content += f"Category: {row['category']}\n"
    content += f"Subcategory: {row['subcategory']}\n"
    content += f"Price: ${row['price']}\n"
    content += f"Description: {row['description']}\n"

    # Write the content to the file
    with open(filepath, 'w', encoding='utf-8') as file:
        file.write(content)

print(f"Created {len(df)} files in ../data/product")